package com.uzai.console.service.mermobilerecharge.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.mermobilerecharge.MerMobileRechargeDto;
import com.uzai.console.dto.mermobilerecharge.MerMobileRechargeQuery;
import com.uzai.console.entity.MerMobileRecharge;
import com.uzai.console.entity.Merchant;
import com.uzai.console.mapper.MerMobileRechargeMapper;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.service.mermobilerecharge.MerMobileRechargeService;
import com.uzai.console.vo.mermobilerecharge.MerMobileRechargeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/***
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class MerMobileRechargeServiceImpl implements MerMobileRechargeService {

    Logger logger = LoggerFactory.getLogger(MerMobileRechargeServiceImpl.class);

    @Autowired
    private MerMobileRechargeMapper merMobileRechargeMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 查询账户余额交易记录列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merMobileRechargeQuery
     * @return null
     */
    public Page<MerMobileRechargeVo> findMerMobileRechargeByPage(MerMobileRechargeQuery merMobileRechargeQuery){
        //新建返回到页面的LIST对象
        List<MerMobileRechargeVo> merMobileRechargeVoList = new ArrayList<>();

        //执行分页查询对象
        Page<MerMobileRecharge> page = new Page<>();
        merMobileRechargeQuery.convert(page);
        ArrayList<MerMobileRecharge> merMobileRechargeList =  merMobileRechargeMapper.findByList(page, merMobileRechargeQuery);
        if(merMobileRechargeList != null && merMobileRechargeList.size() > 0){
            for (MerMobileRecharge merMobileRecharge : merMobileRechargeList){
                MerMobileRechargeVo merMobileRechargeVo = new MerMobileRechargeVo();
                BeanUtils.copyProperties(merMobileRecharge, merMobileRechargeVo);
                //运营商信息
                Merchant merchant = merchantMapper.selectById(merMobileRecharge.getMerId());
                if(merchant != null){
                    merMobileRechargeVo.setMerchantMobile(merchant.getMobile());
                    merMobileRechargeVo.setMerchantName(merchant.getName());
                }
                merMobileRechargeVoList.add(merMobileRechargeVo);
            }
        }
        //返回到页面分页对象
        Page<MerMobileRechargeVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(merMobileRechargeVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 修改运营商手机充值记录状态
     * @param merMobileRechargeDto
     */
    public void updateStatus(MerMobileRechargeDto merMobileRechargeDto){

        if(merMobileRechargeDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "主键id不能为空");
        }

        MerMobileRecharge merMobileRecharge = merMobileRechargeMapper.selectById(merMobileRechargeDto.getId());

        if(merMobileRecharge == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该手机充值记录不存在");
        }

        merMobileRecharge.setStatus(merMobileRechargeDto.getStatus());
        merMobileRecharge.setUpdateTime(DateUtil.getNowTime());

        merMobileRechargeMapper.updateStatus(merMobileRecharge);

    }

}
